home *** CD-ROM | disk | FTP | other *** search
/ BCI NET / BCI NET Dec 94.iso / archives / programming / c / gcc222-3.lha / const_class / sllist.hp < prev    next >
Encoding:
Text File  |  1993-02-14  |  3.1 KB  |  137 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2. /* 
  3. Copyright (C) 1988 Free Software Foundation
  4.     written by Doug Lea (dl@rocky.oswego.edu)
  5.  
  6. This file is part of the GNU C++ Library.  This library is free
  7. software; you can redistribute it and/or modify it under the terms of
  8. the GNU Library General Public License as published by the Free
  9. Software Foundation; either version 2 of the License, or (at your
  10. option) any later version.  This library is distributed in the hope
  11. that it will be useful, but WITHOUT ANY WARRANTY; without even the
  12. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  13. PURPOSE.  See the GNU Library General Public License for more details.
  14. You should have received a copy of the GNU Library General Public
  15. License along with this library; if not, write to the Free Software
  16. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18.  
  19.  
  20. #ifndef _<T>SLList_h
  21. #ifdef __GNUG__
  22. #pragma interface
  23. #endif
  24. #define _<T>SLList_h 1
  25.  
  26. #include <Pix.h>
  27. #include "<T>.defs.h"
  28.  
  29. #ifndef _<T>SLListNode_h
  30. #define _<T>SLListNode_h 1
  31.  
  32. struct <T>SLListNode
  33. {
  34.   <T>SLListNode*         tl;
  35.   <T>                    hd;
  36.                          <T>SLListNode() { }
  37.                          <T>SLListNode(const <T&> h, <T>SLListNode* t = 0);
  38.                          ~<T>SLListNode() { }
  39. };
  40.  
  41.  
  42. inline <T>SLListNode::<T>SLListNode(const <T&> h, <T>SLListNode* t)
  43. :hd(h), tl(t) {}
  44.  
  45. typedef <T>SLListNode* <T>SLListNodePtr;
  46.  
  47. #endif
  48.  
  49.  
  50. class <T>SLList
  51. {
  52. protected:
  53.   <T>SLListNode*        last;
  54.  
  55. public:
  56.                         <T>SLList();
  57.                         <T>SLList(const <T>SLList& a);
  58.                         ~<T>SLList();
  59.  
  60.   <T>SLList&            operator = (const <T>SLList& a);
  61.  
  62.   int                   empty();
  63.   int                   length();
  64.  
  65.   void                  clear();
  66.  
  67.   Pix                   prepend(<T&> item);
  68.   Pix                   append(<T&> item);
  69.  
  70.   void                  join(<T>SLList&);
  71.  
  72.   Pix                   prepend(<T>SLListNode*);
  73.   Pix                   append(<T>SLListNode*);
  74.  
  75.   <T>&                  operator () (Pix p);
  76.   Pix                   first();
  77.   void                  next(Pix& p);
  78.   int                   owns(Pix p);
  79.   Pix                   ins_after(Pix p, <T&> item);
  80.   void                  del_after(Pix p);
  81.  
  82.   <T>&                  front();
  83.   <T>&                  rear();
  84.   <T>                   remove_front();
  85.   int                   remove_front(<T>& x);
  86.   void                  del_front();
  87.  
  88.   void                  error(const char* msg);
  89.   int                   OK();
  90. };
  91.  
  92. inline <T>SLList::~<T>SLList()
  93. {
  94.   clear();
  95. }
  96.  
  97. inline <T>SLList::<T>SLList()
  98. {
  99.   last = 0;
  100. }
  101.  
  102. inline int <T>SLList::empty()
  103. {
  104.   return last == 0;
  105. }
  106.  
  107.  
  108. inline Pix <T>SLList::first()
  109. {
  110.   return (last == 0)? 0 : Pix(last->tl);
  111. }
  112.  
  113. inline void <T>SLList::next(Pix& p)
  114. {
  115.   p = (p == 0 || p == last)? 0 : Pix(((<T>SLListNode*)(p))->tl);
  116. }
  117.  
  118. inline <T>& <T>SLList::operator () (Pix p)
  119. {
  120.   if (p == 0) error("null Pix");
  121.   return ((<T>SLListNode*)(p))->hd;
  122. }
  123.  
  124. inline <T>& <T>SLList::front()
  125. {
  126.   if (last == 0) error("front: empty list");
  127.   return last->tl->hd;
  128. }
  129.  
  130. inline <T>& <T>SLList::rear()
  131. {
  132.   if (last == 0) error("rear: empty list");
  133.   return last->hd;
  134. }
  135.  
  136. #endif
  137.